package com.Bitco.into.activity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.WindowManager;

import com.aries.ui.util.StatusBarUtil;
import com.aries.ui.view.title.TitleBarView;
import com.aries.ui.widget.alert.UIAlertDialog;
import com.Bitco.into.R;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;

import app.yj.channel.ChannelManager;
import app.yj.channel.activity.WebViewActivity;
import app.yj.channel.event.FinishSplashEvent;
import tech.com.commoncore.ApplyName;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.SPUtil;

/**
 * Created: AriesHoo on 2017/8/7 21:41
 * Function:
 * Desc:
 */
public class SplashActivity extends BaseTitleActivity {

    public static final int TIME_DELAY = 2000;  //延迟跳转时间

    @Override
    public void beforeSetContentView() {
        super.beforeSetContentView();
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setLeftTextDrawable(null)
                .setBgColor(Color.TRANSPARENT);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_splash;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initView(Bundle savedInstanceState) {

        if (!isTaskRoot()) {//防止应用后台后点击桌面图标造成重启的假象---MIUI及Flyme上发现过(原生未发现)
            finish();
            return;
        }

        if (!StatusBarUtil.isSupportStatusBarFontChange()) {
            //隐藏状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        requestChannel();
    }

    private void requestChannel() {
        //todo 根据自己App信息进行传（应用简称+月日）
        String appName = ChannelManager.getChannelCode(mContext, ApplyName.ApplyName);
        ChannelManager.request(this, appName, new ChannelManager.ICallback() {
            @Override
            public void showNetWorkError() {
                showNetErrorDialog();
            }

            @Override
            public void goApp() {
                goAPP();
            }
        });
    }


    /**
     * 2.处理APP 自身的逻辑
     */
    private void goAPP() {
        if (SPUtil.contains(this, "HAS-GUIDED")) {
            //如果不是第一次就直接自动登录
            nextJump(1, null, 0, null);
        } else {
            //第一次就进GUIDE 页面
            SPUtil.put(this, "HAS-GUIDED", "has-guided");
            nextJump(0, null, 0, null);
        }
    }

    /************************************退登相关*****************************************/
    private UIAlertDialog netErrorDialog;

    private void showNetErrorDialog() {
        netErrorDialog = new UIAlertDialog
                .DividerIOSBuilder(mContext)
                .setMessage("亲,当前网络不可用,请检查后重试哟....")
                .setNegativeButton("重新尝试", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        requestChannel();
                    }
                })

                .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        SplashActivity.this.finish();
                    }
                })
                .setMessageTextColor(Color.BLACK)
                .setNegativeButtonTextColorResource(R.color.colorPrimary)
                .setPositiveButtonTextColor(Color.BLACK)
                .setCanceledOnTouchOutside(false)
                .create();
        netErrorDialog.setDimAmount(0.6f);
        netErrorDialog.show();
    }


    /**
     * 跳转统一延迟处理.
     *
     * @param type 0:guide页面; 1:主页; 其他:webview
     * @param url
     */
    private void nextJump(int type, final String url, final int toolBarcolor, final String navcolor) {
        if (type == 0) {
            mContentView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //第一次就进GUIDE 页面
                    FastUtil.startActivity(mContext, GuideActivity.class);
                    SplashActivity.this.finish();
                }
            }, TIME_DELAY);
        } else if (type == 1) {
            mContentView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //进入主页
                    FastUtil.startActivity(mContext, MainActivity.class);
                    SplashActivity.this.finish();
                }
            }, TIME_DELAY);

        } else {
            mContentView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //去到微博view页面.
                    startActivity(WebViewActivity.actionToActivity(mContext, url, toolBarcolor, navcolor));
                    SplashActivity.this.finish();
                }
            }, TIME_DELAY);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (netErrorDialog != null) {
            netErrorDialog.dismiss();
            netErrorDialog = null;
        }
    }

    @Subscribe
    public void onEvent(IEvent event) {
        if (event instanceof FinishSplashEvent) {
            mContentView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            }, TIME_DELAY);
        }
    }

}
